<?php
/**
 * ***********************************************************************************
 * 咨询鲤 商户PC端店铺
 * 
 * 文件上传
 * 
 * @author Mr.RJH
 * @date 2025-09-19
 * @email<15213026209@163.com>
 * ***********************************************************************************
 */
namespace app\controller;

use think\facade\Filesystem;
use think\facade\Validate;

use app\BaseController;
use Qcloud\Cos\Client;
use Qcloud\Cos\Exception\ServiceResponseException;

class Upload extends BaseController
{
    private $region_area='';
	private $bucket='';
    private $cosClient;
	/**
	 * 初始化值
	 */
	public function __construct()
	{
		$this->region_area='ap-chongqing';
		$this->cosClient = new Client(
            array(
                'region' => $this->region_area,
                    'credentials'=> array(
                        'appId' => 1253363920,
                        'secretId' => 'AKID2thio2sX0Jh8Tlp7B7vZ7iWgyODXu3nM',
                        'secretKey' => 'ojiciu3JviaLwTrV1NeAnrNl4a9lhUMK'
                    )
            )
        );
    	$this->bucket = 'szh-nrffpt-1253363920';
	}

    /**
     * 图片上传
     * 
     * @return json
     */
    public function uploadImg()
    {
        // 上传本地
        $files = request()->file();
        if (empty($files)) {
            return json(['code' => 1, 'msg' => '请上传图片']);
        }
        $savename = '';
        if(is_array($files)){
            try {
                validate(['image'=>'fileSize:10485760|fileExt:jpg,png,jpeg'])->check($files);

                foreach($files as $file) {
                    $savename = Filesystem::disk('public')->putFile( 'uploads', $file);

                    //腾讯云
                    return $this->uploadQcloud(app()->getRootPath().'public/storage/'.$savename,$savename);
                }

            } catch (\think\exception\ValidateException $e) {
                return json(['code' => 1 , 'msg' => $e->getMessage()]);
            }
        }
        $url = getDomain();
        if( $savename ) {
            $path = $url.'/storage/'.$savename;
            return json(['code' => 0 , 'msg' => '上传成功' , 'data' => $path]);
        }
        else {
            return json(['code' => 1 , 'msg' => '上传失败']);
        }
    }

    /**
     * 上传视频/音频
     * 
     * @return json
     */
    public function uploadVideo()
    {
        // 上传本地
        $files = request()->file();
        if (empty($files)) {
            return json(['code' => 1, '请上传视频']);
        }
        $savename = '';
        try {
            validate(['files'=>'fileSize:104857600|fileExt:mp3,mp4'])->check($files);
            
            foreach($files as $file) {
                $savename = Filesystem::disk('public')->putFile( 'uploads', $file);
                
                //腾讯云
                return $this->uploadQcloud(app()->getRootPath().'public/storage/'.$savename,$savename);
            }
            
        } catch (\think\exception\ValidateException $e) {
            return json(['code' => 1 , 'msg' => $e->getMessage()]);
        }
        $url = getDomain();
        if( $savename ) {
            $path = $url.'/storage/'.$savename;
            return json(['code' => 0 , 'msg' => '上传成功' , 'data' => $path]);
        }
        else {
            return json(['code' => 1 , 'msg' => '上传失败']);
        }
    }

    /**
     * 文件上传
     * 
     * @return json
     */
    public function uploadFile()
    {
        // 上传本地
        $files = request()->file();
       
        if (empty($files)) {
            return json(['code' => 1, '请上传文件']);
        }
        $savename = '';
        if(is_array($files)){
           
            try {
                validate(['files'=>'fileSize:104857600|fileExt:txt,csv,xls,xlsx,pdf,docx,doc,ppt,pptx'])->check($files);
                
                foreach($files as $file) {
                    $savename = Filesystem::disk('public')->putFile( 'uploads', $file);
                    //腾讯云
                    return $this->uploadQcloud(app()->getRootPath().'public/storage/'.$savename,$savename);
                }
            } catch (\think\exception\ValidateException $e) {
                return json(['code' => 1 , 'msg' => $e->getMessage()]);
            }
        }
        if( $savename ) {
            $path = '/storage/'.$savename;
            return json(['code' => 0 , 'msg' => '上传成功' , 'data' => $path]);
        } else {
            return json(['code' => 1 , 'msg' => '上传失败']);
        }
        
    }

    /**
     * 上传腾讯云
     * 
     * @param $file_path 文件路径
     */
    private function uploadQcloud($file_path,$savename,$type=1)
    {
        # 上传文件
        ### 上传文件流
        try {
            $time = rand(1,10000).time();
            
            $file = fopen($file_path,'rb');
            if ($file) {
                $result = $this->cosClient->Upload(
                    $bucket = $this->bucket,
                    $key = '/consult/'.$savename,
                    $body = $file
                );
                
                if(!empty($result)){
                    @unlink($file_path);
                    if ($type == 2) {
                        $data['src'] = 'https://'.$result['Location'];
                    } else {
                        $data = 'https://'.$result['Location'];
                    }
                    return json(['code' => 0, 'msg' =>'上传成功','data' => $data]);
                } else {
                    return json(['code' => 1, 'msg' =>'上传失败']);
                }
            }
        } catch (\Exception $e) {
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
        
    }

    /**
     * 图片本地上传
     *
     * @return json
     */
    public function uploadlocalImg()
    {
        // 上传本地
        $files = request()->file();
        if (empty($files)) {
            return json(['code' => 1, '请上传图片']);
        }
        $savename = '';
        if(is_array($files)){
            try {
                validate(['image'=>'fileSize:10485760|fileExt:jpg,png,jpeg'])->check($files);

                foreach($files as $file) {
                    $savename = Filesystem::disk('public')->putFile( 'uploads', $file);
                }

            } catch (\think\exception\ValidateException $e) {
                return json(['code' => 1 , 'msg' => $e->getMessage()]);
            }
        }
        $url = getDomain();
        if( $savename ) {
            $path = $url.'/storage/'.$savename;
            return json(['code' => 0 , 'msg' => '上传成功' , 'data' => $path]);
        }
        else {
            return json(['code' => 1 , 'msg' => '上传失败']);
        }
    }
    
}